package com.android.app;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Point;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.android.R;


public abstract class BaseTabView {
    private ViewGroup mRootView = null;
    private View v_content;
    private Context mContext;
    private boolean isCreate = true;
    private boolean ifFinish = false;
    private boolean isStop = false;
    private LayoutInflater mInflater;//
    private CharSequence titleName;// 当前选项卡标题
    private Resources mResources;

    public Point getDisplayMetrics() {
        DisplayMetrics dm = new DisplayMetrics();
        dm = mContext.getApplicationContext().getResources()
                .getDisplayMetrics();
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        if (screenHeight > screenWidth) {
            return new Point(screenWidth, screenHeight);
        } else {
            return new Point(screenHeight, screenWidth);
        }
    }

    /**
     * 上下文对象
     *
     * @return 获取上下文对象（可能是Activity）
     */
    public Context getContext() {
        return mContext;
    }

    public ViewGroup getRootView() {
        return mRootView;
    }

    public Resources getResources() {
        if (mResources == null) {
            mResources = mContext.getResources();
        }
        return mResources;
    }

    /**
     * 启动Activity（注意 context 是否是Activity 否则设置flag）
     *
     * @param intent
     */
    public void startActivity(Intent intent) {
        mContext.startActivity(intent);
    }

    /**
     * 查找控件
     *
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    protected <T extends View> T findViewById(int id, Class<T> view) {
        return (T) mRootView.findViewById(id);
    }

    /**
     * 查找控件
     *
     * @param id
     * @return
     */
    @SuppressWarnings("unchecked")
    protected <T extends View> T findViewById(int id) {
        return (T) mRootView.findViewById(id);
    }


    /**
     * 根据string id 获取字符串
     *
     * @param string string.xml字段id
     * @return
     */
    protected String getString(int string) {
        return getResources().getString(string);
    }

    public BaseTabView(Context c) {
        this(c, null);
    }

    public BaseTabView(Context c, CharSequence txt, ViewGroup viewGroup) {
        if(c!=null) {
            this.mContext = c;
            mInflater = LayoutInflater.from(mContext);
            mRootView = (ViewGroup) mInflater.inflate(R.layout.activity_base_main,
                    null, false);
//        loding();
            ViewPager.LayoutParams vl = new ViewPager.LayoutParams();
            vl.width = ViewPager.LayoutParams.MATCH_PARENT;
            vl.height = ViewPager.LayoutParams.MATCH_PARENT;
            mRootView.setLayoutParams(vl);
            if (txt != null) {
                titleName = txt;
            } else {
                titleName = getTitle();
            }
        }
    }

    public BaseTabView(Context c, ViewGroup viewGroup) {
        this(c, null, viewGroup);
    }

    /**
     * 获取当前tab的标题
     *
     * @return
     */
    public CharSequence getTabTitle() {
        return titleName;
    }

    public void setTabTitle(String title){
        this.titleName = title;
    }

    /**
     * 设置标题
     *
     * @return 标题名称
     */
    protected CharSequence getTitle() {
        return "tab";
    }


    protected abstract int getContentView();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initListener();


    /**
     * 控件恢复处理当前tab信息
     */
    public void onResume() {
        isStop = false;
    }

    /**
     * 当前控件停止
     */
    public void onStop() {
        isStop = true;
    }

    public void onDestroy() {
        isStop = true;
    }

    /**
     * 当前控件暂停
     */
    public void onPause() {
        isStop = true;
    }

    /**
     * 当窗口得到焦点
     */
    public void onWindowFocusChanged(boolean focus) {
        if (focus) {
            isStop = false;
            if (isCreate && !isStop) {
                isCreate = false;
                mRootView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (!isStop) {
                            setContentView(getContentView());
                            initView();
                            initListener();
                            initData();
                            onResume();
                            isCreate = false;
                            ifFinish = true;
                            hide();
                        } else {
                            isCreate = true;
                            ifFinish = false;
                        }
                    }
                }, 500);
                hide();
            } else {
                mRootView.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (ifFinish) {
                            onResume();
                        }
                    }
                }, 1);
            }
        }
    }

    /**
     * 设置当前tab的主窗口
     *
     * @param v 独立的控件
     */
    protected void setContentView(View v) {
        mRootView.addView(v, 0);
    }

    /**
     * 设置当前tab的主窗口
     *
     * @param layout layout
     */
    protected void setContentView(int layout) {
        try {
            v_content = mInflater.inflate(layout, null, false);
            mRootView.addView(v_content);
        } catch (Exception e) {
            e.printStackTrace();
        }

        // mRootView.addView(mInflater.inflate(layout,
        // (ViewGroup) mRootView.findViewById(R.id.tab_rootView),
        // mRootView.findViewById(R.id.tab_rootView) != null));
    }

    public void loding() {
        View loding = findViewById(R.id.fl_progressbar, View.class);
        loding.setVisibility(View.VISIBLE);
        if (v_content != null)
            v_content.setVisibility(View.GONE);
    }

    public void hide() {
        if (v_content != null)
            v_content.setVisibility(View.VISIBLE);

        View loding = findViewById(R.id.fl_progressbar, View.class);
        loding.setVisibility(View.GONE);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    }



    public void dispatchTouchEvent(MotionEvent ev) {
    }


    public <T extends View> T $(int id) {
        return (T) findViewById(id);
    }
}
